import {wxRequest} from '../../utils/server.js';
import store_share from "../../component/painter/palette/store";
const storeShare = new store_share() 
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'), 
    adList: [], 
    id: '',
    swiperList: [],  
    arrt: false,
    show: false,
    mdc_show: false,
    playIndex: '',
    shareStart:false
  },

  onLoad: function (option) {
    var pramas = option

    //通过分享进来
    if (option.scene) {
      pramas = _wxRequest.urlScene(decodeURIComponent(option.scene))
      pramas.inviteStoId = pramas.id
      console.log(pramas, 'scene')
    }
 
    this.setData({
      id: pramas.id,
      inviteStoId: pramas.inviteStoId || '',
      sharedCusId: pramas.sharedCusId || '', 
    })
 
    if (pramas.inviteStoId) {
      wx.removeStorageSync('sharedCusId')
      wx.setStorageSync('inviteStoId', pramas.inviteStoId)
    }

    if (pramas.sharedCusId) {
      wx.removeStorageSync('inviteStoId')
      wx.setStorageSync('sharedCusId', pramas.sharedCusId)
    }

  },
  onShow: function () {
    this.getDetail(); 
    if (wx.getStorageSync('Authorization')) {
      this.getAttend();//查询关注
    } 
  },

  //获取详情
  getDetail: function () {
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/org/store/detail?id=' + this.data.id,
      method: 'POST'
    }, (res) => {
      var detail = res.vo;
      if (detail.offInfoJson){
        detail.offInfoJson = JSON.parse(detail.offInfoJson)
      } 
      this.setData({
        detail: detail,
        adList: res.vo.coverImgs
      })
    })

  },
 


  //预览户型图
  imageList: function (e) {
    let current = this.data.uploadUrl + e.target.dataset.src;
    let urls = e.target.dataset.urls;
    if (urls && urls.length) {
      for (var i = 0; i < urls.length; i++) {
        urls[i] = this.data.uploadUrl + urls[i].imgPath
      };
    }
    wx.previewImage({
      current: current, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    });
  },

  //分享 
  onShareAppMessage: function () {
    this.setData({
      shareStart: !this.data.shareStart
    })
    var image = this.data.uploadUrl + this.data.detail.coverImgs[0].imgPath;
    var uId = wx.getStorageSync('userInfo').id || ''
    return {
      title: this.data.detail.stoName,
      imageUrl: image,
      path: '/pages/store/detail?id=' + this.data.id + '&sharedCusId=' + wx.getStorageSync('userInfo').id || ''
    }
  },
 

  //添加关注 
  customerAttend: function () {
    var that = this;
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/storeAttend/addCustomerAttend?stoId=' + this.data.id,
      method: 'post'
    }, (res) => {
      that.setData({
        arrt: true
      })

      wx.showToast({
        icon: 'none',
        title: '收藏成功'
      })
    })
  },

  //取消关注
  delCustomerAttend: function () {
    var that = this;
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/storeAttend/deleteCustomerAttend',
      method: 'post',
      contentType: 'application/x-www-form-urlencoded',
      data:{
        stoId: this.data.id,
        stoIdList: [this.data.id]
      }
    }, (res) => {
      that.setData({
        arrt: false
      })
      wx.showToast({
        icon: 'none',
        title: '取消收藏'
      })
    })
  },


  // 查询关注
  getAttend: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/storeAttend/existsCustomerAttend?stoId=' + this.data.id,
      method: 'POST'
    }, (res) => {
      this.setData({
        arrt: res.flag
      })
    })
  },
 
  toArticle: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/activity/detail?id=' + id,
    })
  },


  //打电话
  toTel: function (e) {
    var mobile = e.currentTarget.dataset.mobile;
    wx.makePhoneCall({
      phoneNumber: mobile,
    })
  },


  //地图导航
  goMap: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        wx.openLocation({
          latitude: Number(that.data.detail.lat),//要去的纬度-地址
          longitude: Number(that.data.detail.lng),//要去的经度-地址
          name: that.data.detail.detailAddress,
          address: that.data.detail.detailAddress
        })
      }
    })
  },


  bindPlay: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.index 
    // 没有播放时播放视频
    if (!this.data.playIndex) {
      this.setData({
        playIndex: id
      })
      var videoContext = wx.createVideoContext(id) //这里对应的视频id
      videoContext.play()
    } else { // 有播放时先将prev暂停，再播放当前点击的current 
      var videoContextPrev = wx.createVideoContext(this.data.playIndex)
      if (this.data.playIndex != id) {
        videoContextPrev.pause()
      }
      this.setData({
        playIndex: id
      })
      var videoContextCurrent = wx.createVideoContext(id)
      videoContextCurrent.play()
    }


    that.setData({
      mdc_show: true
    })

  },

  outPlay: function () {
    this.setData({
      mdc_show: false
    })
  },



  //安卓系统能检测到 video touchemove 滑动的x距离，已此作为切换的swiper的凭证
  //ios系统，检测不到video touchemove 滑动的x距离，通过cover-view 点击事件进行切换
  mdc_move1: function (e) {
    var that = this;
    // console.log(e)
    // console.log(e.touches[0].pageX)
    var videoContextPrev = wx.createVideoContext('mdcVideo1');
    videoContextPrev.pause()
    if (e.touches[0].clientX > 20) {
      that.setData({
        mdc_show: false,
      })
    }
  },
  returnquanping: function (e) {
    var that = this;
    that.setData({
      mdc_show: false,
    })
  },

 

  //点击分享
  clickShare: function () {
    this.setData({
      shareStart: !this.data.shareStart
    })
  },

  //下载 
  saveImg: function () {
    var that = this; 
    this.setData({
      shareStart: !this.data.shareStart
    })
    wx.showToast({
      title: '素材加载中',
      icon: 'loading'
    })
    var n = 0
    for (var i = 0; i < that.data.adList.length; i++) {
      wx.getImageInfo({ //将获取图片的信息
        src: that.data.uploadUrl + that.data.adList[i].imgPath,// 需要下载的图片
        success(res) {
          var filePath = res.path //得到本地的路径 
          wx.saveImageToPhotosAlbum({
            filePath: filePath,
            success(res) {
              n++
              if (n == that.data.adList.length) {
                wx.showToast({
                  title: '素材下载成功',
                  icon: 'success'
                })
              }
            }
          })
        }
      })

    }
  },


  //获取短码encodeShort
  decodeShort() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/comm/encodeShort',
      method: 'POST',
      data: {
        longCode: 'id=' + this.data.id + '&sharedCusId=' + wx.getStorageSync('userInfo').id 
      }

    }, (res) => {
      this.getCode(res.shortCode)
    })
  },

  //获取二维吗
  getCode() {
    var that = this
    this.setData({
      shareStart: !this.data.shareStart
    })
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/odr/mall/getWxaCodeUnlimit',
      contentType: "application/x-www-form-urlencoded",
      method: 'POST',
      data: {
        page: '/pages/store/detail',
        width: 750,
        scene: 'id=' + this.data.id
      }
    }, (res) => {
      console.log(res, 'getCode')
      that.setData({
        qrCode: res.url
      }, () => {
        that.toShare()
      })
    })
  },


  //分享
  toShare() {
    var qrCode = this.data.qrCode
    this.setData({
      shareImgTemplate: storeShare.palette(qrCode, this.data.detail, 0)
    });
    wx.showLoading({
      title: "努力生成中..."
    });
  },

  //图片生成完成后呈现在页面上
  onShareImgOK(e) {
    this.setData({
      shareImgUrl: e.detail.path
    });
    wx.hideLoading();
    this.previewShareImg();
  },


  previewShareImg(e) {
    let url = this.data.shareImgUrl;
    wx.previewImage({
      current: url,
      urls: [url]
    });
  },








  
})